package com.javasm.gg;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.TableDatas;
import com.javasm.commons.enums.E;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("supplier")
public class SupplierController extends BaseController {
    @Autowired
    private SupplierService supService;

    @GetMapping
    public TableDatas getallsupplierbypage(Supplier supplier) {
        startPage();
        List<Supplier> list = supService.list(new QueryWrapper<Supplier>(supplier));
        return toTableDatas(list);
    }

    @PostMapping
    public AxiosResult insert(@RequestBody Supplier supplier) {
        boolean save = supService.save(supplier);
        if (save) return AxiosResult.suc();
        else return AxiosResult.error();
    }

    @PutMapping
    public AxiosResult updateByid(@RequestBody Supplier supplier) {
        boolean b = supService.updateById(supplier);
        if (b) return suc();
        else return AxiosResult.error();
    }
    @PostMapping("setGoods2Supplier")
    public AxiosResult setGoods2Supplier(@RequestBody Map<String,String> map){
        String sid=map.get("sid");
        String gids = map.get("gids");
        boolean isok = supService.setGoods2Supplier(sid,gids.split(","));
        if(isok) return suc();
        else return AxiosResult.error();
    }

    @DeleteMapping("{ids}")
    public AxiosResult delById(@PathVariable String ids) {
        String[] myid = ids.split(",");
        boolean b = supService.removeByIds(Arrays.asList(myid));
        if (b) return AxiosResult.suc();
        else return AxiosResult.error();
    }

    @GetMapping("getByid")
    public AxiosResult getsupplierByid(Integer id) {
        Supplier supplier = supService.getById(id);
        if(supplier!=null){
            return suc(supplier);
        } else {
            return AxiosResult.error(E.NOT_EXISTS);
        }
    }

    @GetMapping("getSupplierName")
    public AxiosResult getSupplierName(Supplier supplier) {
//        List<Supplier> supplierName = supService.getSupplierName();
//        if(supplierName!=null){
//            return AxiosResult.suc(supplierName);
//        }else {
//            return AxiosResult.error(E.NOT_EXISTS);
//        }
        return null;
    }

    @GetMapping("getGoodsBySuplierid")
    public AxiosResult getGoodsBysuplierid(Integer id) {
        List<Goods> goodsList = supService.listGoodsBySuplierid(id);
        return suc(goodsList);
    }

    @DeleteMapping("delBySidAndGid")
    public AxiosResult delBySidAndGid(Integer sid, Integer gid) {
//        int delResult = supService.delBySidAndGid(sid,gid);
//        if (delResult != 0) {
//            return AxiosResult.suc(E.SUC);
//        } else {
//            return AxiosResult.error(E.OPERATION_FAILED);
//        }
        return null;
    }
}
